package com.patelski.common.service;

import java.util.List;

import javax.ejb.Remove;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.annotations.datamodel.DataModelSelection;
import org.jboss.seam.log.Log;

public abstract class AbstractSearchAction<T> implements Searchable<T> {

	@DataModelSelection
	public T instance;

	@DataModel
	protected List<T> resultList;

	@Logger
	protected Log log;

	@PersistenceContext(type = PersistenceContextType.EXTENDED)
	protected EntityManager em;
	protected int pageSize = 10;
	protected int page;

	public AbstractSearchAction() {
		super();
	}

	protected abstract void query();

	public void find() {
		page = 0;
		query();
	}

	public void nextPage() {
		page++;
		query();
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	@Remove
	public void destroy() {
	}

	public List<T> getResultList() {
		return resultList;
	}

	public void setResultList(List<T> resultList) {
		this.resultList = resultList;
	}

	public T getInstance() {
		return instance;
	}

	public void setInstance(T instance) {
		this.instance = instance;
	}

	public boolean isNextPageAvailable() {
		return resultList != null && resultList.size() == pageSize;
	}

}